package com.centaline.androidsalesblog.fragments.entrust;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.api.usercenter.ReservationListApi;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.model.businissobject.ReservationBo;
import com.centaline.androidsalesblog.model.businissobject.ReservationParentBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.model.responseobject.PostListRo;
import com.centaline.androidsalesblog.model.responseobject.ReservationRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

/**
 * 约看列表基类
 * Created by guilin on 16/4/23.
 */
public abstract class BaseReservationFragment extends BaseFragment {

    protected ArrayList<ReservationBo> reservationBoArrayList = new ArrayList<>();
    protected ArrayList<ReservationParentBo> reservationParentBoArrayList = new ArrayList<>();
    protected ArrayList<String> postIdArrayList = new ArrayList<>();//保存最初的post顺序
    protected ReservationListApi reservationListApi;
    protected boolean isRequest;//是否已请求

    @Override
    protected void initView() {
        reservationListApi = new ReservationListApi(getClass(),
                ReservationRo.class,
                new IResponseListener<ReservationRo>() {
                    @Override
                    public void onResponse(ReservationRo reservationRo) {
                        ArrayList<ReservationBo> temp = reservationRo.getReservationBos();
                        reservationBoArrayList.clear();
                        reservationParentBoArrayList.clear();
                        postIdArrayList.clear();
                        if (0 != reservationRo.getResultNo() ||
                                temp == null ||
                                temp.size() == 0) {
                            noData();
                        } else {
                            response(temp);
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        responseError();
                    }
                });
        reservationListApi.setReserve(isReserve());
    }

    /**
     * 请求约看列表
     */
    protected void requestReservationList() {
        reservationListApi.startRequest();
    }

    /**
     * 是否约看
     */
    protected abstract boolean isReserve();

    /**
     * 约看数据
     */
    protected void response(ArrayList<ReservationBo> temp) {
        reservationBoArrayList.addAll(temp);
        StringBuilder ids = new StringBuilder();
        for (ReservationBo reservationBo : reservationBoArrayList) {
            ids.append(reservationBo.getPostID()).append(",");
            postIdArrayList.add(reservationBo.getPostID());
        }
        if (ids.length() > 0) {
            ids.deleteCharAt(ids.length() - 1);
            requestPostList(ids.toString());
        }
    }

    /**
     * 请求租售列表
     */
    protected void requestPostList(String ids) {
        PostListApi postListApi = new PostListApi(getClass(), PostListRo.class, new IResponseListener<PostListRo>() {
            @Override
            public void onResponse(PostListRo postListRo) {
                responsePostList(postListRo.getList());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                responseError();
            }
        });
        postListApi.setPostIds(ids);
        postListApi.startRequest();
    }

    /**
     * 租售列表
     */
    protected abstract void responsePostList(ArrayList<PostDo> temp);

    /**
     * 没有数据
     */
    protected abstract void noData();

    protected void responseError() {
        netWorkUnable();
    }
}
